package net.fxug.hokuriku.chocolat.helper
{
    import flash.events.Event;

    import mx.core.IMXMLObject;
    import mx.events.FlexEvent;

    import net.fxug.hokuriku.chocolat.events.SessionTimerEvent;
    import net.fxug.hokuriku.chocolat.events.ViewChangeEvent;
    import net.fxug.hokuriku.chocolat.utils.FilterUtil;
    import net.fxug.hokuriku.chocolat.view.SessionTimeUp;
    import net.fxug.hokuriku.chocolat.view.SpeakerView;
    import net.fxug.hokuriku.chocolat.view.TimelineTimer;

    /**
     * スピーカー画面のヘルパークラス
     *
     * @author shoito
     */
    public class SpeakerViewHelper implements IMXMLObject
    {
        private var view:SpeakerView;
        private var timelineTimer:TimelineTimer;
        private var sessionTimeUp:SessionTimeUp;

        public function initialized(document:Object, id:String):void
        {
            this.view = document as SpeakerView;
            this.view.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
            this.view.filters = [FilterUtil.getDropShadowFilter()];
        }

        private function creationCompleteHandler(event:FlexEvent):void
        {
            this.view.setting.addEventListener(SessionTimerEvent.START, setting_timerStartHandler);
            this.view.setting.addEventListener(ViewChangeEvent.BACK, setting_backHandler);
            this.view.addEventListener(FlexEvent.REMOVE, removeHandler);
        }

        private function setting_timerStartHandler(event:Event):void
        {
            timelineTimer = new TimelineTimer();
            timelineTimer.addEventListener(Event.CLOSE, timelineTimer_closeHandler);
            timelineTimer.visible = true;

            this.view.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.HIDE));
            this.view.currentState = "score";
        }

        private function setting_backHandler(event:Event):void
        {
            this.view.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.BACK));
        }

        private function removeHandler(event:FlexEvent):void
        {
            this.view.currentState = "setting";
        }

        private function timelineTimer_closeHandler(event:Event):void
        {
            sessionTimeUp = new SessionTimeUp();
            sessionTimeUp.addEventListener(Event.CLOSE, timeUpWindow_closeHandler);
            sessionTimeUp.visible = true;
        }

        private function timeUpWindow_closeHandler(event:Event):void
        {
            this.view.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.SHOW));
            this.view.score.dispatchEvent(new ViewChangeEvent(ViewChangeEvent.SHOW));
        }
    }
}